package com.tpshion.cloud.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tpshion.cloud.auth.domain.dto.*;
import com.tpshion.cloud.auth.domain.entity.Permission;
import com.tpshion.cloud.auth.domain.vo.PermissionVo;
import com.tpshion.cloud.auth.mapper.PermissionMapper;
import com.tpshion.cloud.auth.service.PermissionService;
import com.tpshion.cloud.common.support.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功能权限管理
 */
@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 分页查询功能权限
     * @param queryPermissionDto
     * @return
     */
    @PostMapping(value = "/permissions")
    public Result<?> queryPermission(@RequestBody @Validated QueryPermissionDto queryPermissionDto){
        PageInfo pageInfo = permissionService.queryPermission(queryPermissionDto);
        return Result.ok(pageInfo);
    }

    /**
     * 检查权限是否已存在
     */
    @PostMapping(value = "/permission/checkName")
    public Result<?> checkName(@RequestBody @Validated PermissionCheckNameDto permissionCheckNameDto){
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", permissionCheckNameDto.getName());
        if(!StringUtils.isEmpty(permissionCheckNameDto.getId())){
            queryWrapper.ne("id", permissionCheckNameDto.getId());
        }
        Integer count = permissionMapper.selectCount(queryWrapper);
        if(count > 0){
            return Result.fail("权限已存在");
        }
        return Result.ok();
    }

    /**
     * 检查接口路径是否已存在
     */
    @PostMapping(value = "/permission/checkPath")
    public Result<?> checkPath(@RequestBody @Validated PermissionCheckPathDto permissionCheckPathDto){
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("path", permissionCheckPathDto.getPath());
        if(!StringUtils.isEmpty(permissionCheckPathDto.getId())){
            queryWrapper.ne("id", permissionCheckPathDto.getId());
        }
        Integer count = permissionMapper.selectCount(queryWrapper);
        if(count > 0){
            return Result.fail("接口路径已存在");
        }
        return Result.ok();
    }

    /**
     * 添加权限
     */
    @PostMapping(value = "/permission", params = {"action=add"})
    @com.tpshion.cloud.core.annotation.Permission("auth:add")
    public Result<?> addPermission(@RequestBody @Validated AddPerMissionDto addPerMissionDto){
        // 检验权限是否已存在
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", addPerMissionDto.getName());
        queryWrapper.eq("path", addPerMissionDto.getPath());
        Integer count = permissionMapper.selectCount(queryWrapper);
        if(count > 0){
            return Result.fail("权限已存在");
        }
        Permission permission = permissionService.addPermission(addPerMissionDto);
        return Result.ok(permission);
    }

    /**
     * 更新权限信息
     */
    @PostMapping(value = "/permission", params = {"action=upd"})
    @com.tpshion.cloud.core.annotation.Permission("auth:update")
    public Result<?> updatePermission(@RequestBody @Validated UpdatePermissionDto updatePermissionDto){
        Permission permission = permissionService.updatePermission(updatePermissionDto);
        return Result.ok(permission);
    }

    /**
     * 删除权限
     */
    @PostMapping(value = "/permission", params = {"action=del"})
    @com.tpshion.cloud.core.annotation.Permission("auth:del")
    public Result<?> deletePermission(@RequestBody List<String> ids){
        if(CollectionUtils.isEmpty(ids)){
            return Result.fail("参数不能为空");
        }
        permissionService.deletePermission(ids);
        return Result.ok();
    }

    /**
     * 获取固定格式所有权限信息
     */
    @GetMapping(value = "/permissions")
    public Result<?> getAllPermission(){
        List<PermissionVo> permissionVos = permissionMapper.findAll();
        return Result.ok(permissionVos);
    }

}
